[#ftl]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>[#assign module]project/${item.scope.id}/mtmrelations[/#assign][#assign moduleName="多对多关系"]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>[#if (item.id)??]编辑${moduleName}信息[#else]添加新${moduleName}[/#if]</title>
<script type="text/javascript" src="${base}/commonjs/jquery1.7.2/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="${base}/commonjs/jquery-forms.js"></script>
<script type="text/javascript" src="${base}/commonjs/jquery.select.js"></script>
<script type="text/javascript" src="${base}/commonjs/artDialog4/artDialog.source.js?skin=blue"></script>
<script type="text/javascript" src="${base}/commonjs/artDialog4/plugins/iframeTools.source.js"></script>
<script type="text/javascript">
$(function(){
	[#if (item.masterField.id)??][#assign createMode=1][#else][#assign createMode=0][/#if]
	//简单模式的数据获取
	var masterId = ${(item.master.id)!0};
	var slaveId = ${(item.slave.id)!0};
	var simpleModeInited = false;
	function initMTMSimple(masterId, slaveId){
		if(simpleModeInited){
			return;
		}
		simpleModeInited = true;
		masterId = masterId||0;
		slaveId = slaveId||0;
		var classes = [];
		$("#selectMaster").createSelect("${base}/project/${item.scope.id}/classes/list", {
	        pageNo:1,pageSize:999
	    }, function(row){
	    	var data = [row.id, row.id+":"+row.cell[1]];
	    	classes.push(data);
	        return data;
	    }, masterId, function(){
	    	 $("#selectSlave").createSelect(classes, slaveId);
	    });
	}
	
	//复杂模式的数据获取
	var candidateClassId = ${(item.relation.id)!0};//默认选中的relation
    var masterFieldId = ${(item.masterField.id)!0};
    var slaveFieldId = ${(item.slaveField.id)!0};
    var complextModeInited = false;
    function initMTMComplext(relationId, masterFieldId, slaveFieldId){
        if(complextModeInited){
            return;
        }
        complextModeInited = true;
    	relationId = relationId||0;
	    $("#selectRelation").createSelect("${base}/project/${item.scope.id}/classes/mtmcandidates", {
	        pageNo:1,pageSize:999
	    }, function(row){
	        var data = [row.id, row.id+":"+row.cell[1]];
	        return data;
	    }, candidateClassId, function(){
	    	initMasterFieldSelect(candidateClassId, masterFieldId, slaveFieldId);
	    }).change(function(){
	        var value = $(this).val();
	        if(value==0){
	        	$("#selectMasterField").attr("disabled", true).val(0);
	        	$("#selectSlaveField").attr("disabled", true).val(0);
	        	
	        }else{
	        	$("#selectMasterField").attr("disabled", false);
	        	$("#selectSlaveField").attr("disabled", false);
	        	if(candidateClassId!=value){
	        		initMasterFieldSelect(value);
	        		candidateClassId=value;
	        	}
	        }
	    });
    }
    [#if createMode==0]initMTMSimple(masterId, slaveId)[#else]initMTMComplext(candidateClassId, masterFieldId, slaveFieldId)[/#if]
    
    function initMasterFieldSelect(classId, masterFieldId, slaveFieldId){
    	masterFieldId = masterFieldId||0;
    	slaveFieldId = slaveFieldId||0;
    	var attributes = [];
    	var selectMasterField = $("#selectMasterField");
    	selectMasterField.get(0).options.length=1;
    	var selectSlaveField = $("#selectSlaveField");
    	selectSlaveField.get(0).options.length=1;
    	selectMasterField.createSelect("${base}/class/"+classId+"/attributes/mtm", {
            pageNo:1,pageSize:999
        }, function(row){
            var data = [row.id, row.id+":"+row.cell[1]+"("+row.cell[2][0]+":"+row.cell[2][1]+")"];
            attributes.push(data);
            return data;
        }, masterFieldId, function(){
        	selectSlaveField.createSelect(attributes, slaveFieldId);
        });
    }
    
    $("#radioMTMSimple").click(function(){
        $("#selectMaster").attr("disabled", false);
        $("#selectSlave").attr("disabled", false);
        $("#selectRelation").attr("disabled", true);
        $("#selectMasterField").attr("disabled", true);
        $("#selectSlaveField").attr("disabled", true);
        initMTMSimple();
    });
   
    $("#radioMTMComplex").click(function(){
        $("#selectMaster").attr("disabled", true);
        $("#selectSlave").attr("disabled", true);
        $("#selectRelation").attr("disabled", false);
        $("#selectMasterField").attr("disabled", false);
        $("#selectSlaveField").attr("disabled", false);
        initMTMComplext();
    });
   
	$("#btadditem").bind("click", function(){
		$("#additemform").ajaxForm({
			dataType: "json",
			success: function(json){
				if(!json.success){
					art.dialog.through({
						title: "[#if (item.id)??]编辑${moduleName}信息[#else]添加新${moduleName}[/#if]失败",
						icon: "error",
						content: "[#if (item.id)??]编辑${moduleName}信息[#else]添加新${moduleName}[/#if]失败！<br>"+json.err,
						width: 250,
						height: 150,
						background: '#EEE', // 背景色
						opacity: 0.05,	// 透明度
						lock: true,
						ok: true
					});
				}else{
					try{
						art.dialog.top.frames["_iframe"].window.flexGridReload(".flexme3");
					}catch(e){
					   try{
					       art.dialog.open.origin.flexGridReload(".flexme3");
					   }catch(e){
					       art.dialog.top.window.flexGridReload(".flexme3");
					   }
					}
					art.dialog.close();
				}
			}
		});
	});
});
</script>
</head>
<body>
<h3>[#if (item.id)??]编辑${moduleName}信息[#else]添加新${moduleName}[/#if]</h3>
<form id="additemform" action="${base}/${module}/${((item.id)??)?string('edit','add')}" method="POST">
<input type="hidden" name="id" value=${(item.id)!}>
<fieldset>
    <legend>
        <input type="radio" id="radioMTMSimple" value="0" name="createMode"${(createMode==0)?string(' checked', '')}>
        <label for="radioMTMSimple">创建简单多对多</label>
    </legend>
主类：<select id="selectMaster" name="master.id"${(createMode==0)?string('', ' disabled')}><option value=0>--请选择主类--</option></select>&nbsp;&nbsp;
从类：<select id="selectSlave" name="slave.id"${(createMode==0)?string('', ' disabled')}><option value=0>--请选择从类--</option></select><br>
</fieldset>
<fieldset>
    <legend>
        <input type="radio" id="radioMTMComplex" value="1" name="createMode"${(createMode==1)?string(' checked', '')}>
            <label for="radioMTMComplex">创建复杂多对多</label>
    </legend>
关系类：<select id="selectRelation" name="relation.id"${(createMode==1)?string('', ' disabled')}><option value=0>--请选择关系类--</option></select><br>
主属性：<select id="selectMasterField" name="masterField.id"${(createMode==1)?string('', ' disabled')}><option value=0>--请选择主属性--</option></select><br>
从属性：<select id="selectSlaveField" name="slaveField.id"${(createMode==1)?string('', ' disabled')}><option value=0>--请选择从属性--</option></select>
</fieldset>
数据处理：
<input type="radio" id="radioTypeBoth" value="0" name="type"${(item.type==0)?string(' checked', '')}><label for="radioTypeBoth">内外连接</label>
<input type="radio" id="radioTypeOuter" value="1" name="type"${(item.type==1)?string(' checked', '')}><label for="radioTypeOuter">仅外连接</label>
<input type="radio" id="radioTypeInner" value="2" name="type"${(item.type==2)?string(' checked', '')}><label for="radioTypeInner">仅内连接</label>
<br>
<br>
<input type="submit" value="[#if (item.id)??]保存${moduleName}信息[#else]保存新${moduleName}[/#if]" id="btadditem">
</form>
</body>
</html>